﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;


namespace PhysicsApplication
{
    public partial class MainForm : Form
    {
        private Engine localEngine = new Engine();
        private List<TrajectoryElements> dataPoints;

        public MainForm()
        {
            InitializeComponent();
        }
        private void zedGraphControl1_Load(object sender, EventArgs e)
        {
            ZedGraph.PointPairList plotPoints = new PointPairList();
            //clear any old values
            CurveList currentList = zedGraphControl1.GraphPane.CurveList;
            currentList.Clear();
            foreach (TrajectoryElements temp in dataPoints)
            {
                double range = Converters.metersToYards( temp.range );
                double alt = Converters.metersToFeet( temp.altitude );
                plotPoints.Add(temp.range, temp.altitude);
            }
            //// get a reference to the GraphPane
            GraphPane myPane = zedGraphControl1.GraphPane;
            myPane.AddCurve("Projectile", plotPoints, Color.Red, SymbolType.None);
            //// Set the Titles
            myPane.Title.Text = "Ballistic Results";
            myPane.XAxis.Title.Text = "Range (feet)";
            myPane.YAxis.Title.Text = "Altitude (feet)";
            zedGraphControl1.AxisChange();
            zedGraphControl1.RestoreScale(myPane);
        }
        private void velocityTextBox_TextChanged(object sender, EventArgs e)
        {
            if (localEngine.setMuzzleVelocityAsFeet(targetRangeTextBox.Text) != true)
            {
                targetRangeTextBox.Text = "0";
                localEngine.setMuzzleVelocityAsFeet("0");
            }
        }
        private void heightTextBox_TextChanged(object sender, EventArgs e)
        {
            if (localEngine.setHeightAsFeet(heightTextBox.Text) != true)
            {
                heightTextBox.Text = "0";
            }
        }
        private void elevationTextBox_TextChanged(object sender, EventArgs e)
        {
            if (localEngine.setElevationAsDegrees(elevationTextBox.Text) != true)
            {
                elevationTextBox.Text = ".0";
                localEngine.setElevationAsDegrees(".0");
            }
        }
        private void PlotButton_Click(object sender, EventArgs e)
        {
            dataPoints = localEngine.inegrate();
            zedGraphControl1_Load(sender, e);
            populateDataTable();
            
        }
        private void populateDataTable()
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            style.Format = "N2";
            dataGridView1.DefaultCellStyle = style;

            // New table.
            DataTable table = new DataTable();                 
            // Add columns.
            table.Columns.Add("Time (sec)");
            table.Columns.Add("Range (feet)");
            table.Columns.Add("Altitude (feet)");
            table.Columns.Add("Velocity (fps)");
            table.Columns.Add("Energy (n)");
            // Add rows.
            foreach (var array in dataPoints)
            {
                DataRow temp = table.NewRow();
                temp.SetField(0, Math.Round(array.elementTime,8));
                temp.SetField(1, Math.Round(array.range,8));
                temp.SetField(2, Math.Round(array.altitude,8));
                temp.SetField(3, Math.Round(array.velocity,8));
                temp.SetField(4, Math.Round(array.kineticEnergy,8));             
                table.Rows.Add(temp);
            }
            dataGridView1.DataSource = table;
            dataGridView1.AutoResizeColumns();
        }
        private void targetRangeTextBox_TextChanged(object sender, EventArgs e)
        {
            if (localEngine.setRangeAsYards(targetRangeTextBox.Text) != true)
            {
                targetRangeTextBox.Text = ".0";
                localEngine.setRangeAsYards(".0");
            }
        }
        private void integrateTimeTextBox_TextChanged(object sender, EventArgs e)
        {
            if (localEngine.setIntegrateTime(integrateTimeTextBox.Text) != true)
            {
                integrateTimeTextBox.Text = "0.001";
                localEngine.setIntegrateTime("0.001");
            }
        }
    }
}
